package DSystem.Client;

import java.io.BufferedReader;
import java.lang.Math;
import java.util.Date;
import java.util.Random;
import java.io.FileReader;
import java.io.IOException;


public class Reader {
	private BufferedReader read;
	private int Nrep;
	private float bInterval;
	private float bLatency;
	private Address[] listAddress;
	private int Ninv;
	
	//faz a leitura do arquivo configuração e alimenta todo reader
	public Reader(String nameFile) throws NumberFormatException, IOException {
		read = new BufferedReader(new FileReader(nameFile));
		Nrep = retNum();
		listAddress = new Address[Nrep];
		for(int i = 0; i< Nrep; i++){
			String[] line = read.readLine().split(" ");
			listAddress[i]=new Address(line[0],Integer.valueOf(line[1]));
		}
		bInterval = retNum();
		bLatency = retNum();
		setNinv(retNum());
		read.close();
	}
	
	private int retNum() throws NumberFormatException, IOException{
		return Integer.valueOf(read.readLine());
	}
	
	public int getNrep(){
		return Nrep;
	}
	
	public Address[] getListAddress() {
		return listAddress;
	}
	
	public float getBInterval(){
		return bInterval;
	}
	
	public float getBLatency(){
		return bLatency;
	}
	
	//devolve e altera o interval
	public float getAndSetBInterval(){
		float ret = bInterval;
		setBInterval();
		return ret;
	}
	
	//devolve e altera o latency
	public float getAndSetBLatency(){
		float ret = bLatency;
		setBLatency();
		return ret;
	}
	
	//modifica o interval para a proxima utilização 
	private void setBInterval(){
		Date currentDate = new Date();
		Random ui = new Random(currentDate.getTime());
		bInterval = (float) (-bInterval * Math.log(ui.nextFloat()));
	}
	
	//modifica o latency para a proxima utilização
	private void setBLatency(){
		Date currentDate = new Date();
		Random uj = new Random(currentDate.getTime());
		bLatency = (float) (-bLatency * Math.log(uj.nextFloat()));
	}

	public int getNinv() {
		return Ninv;
	}

	public void setNinv(int ninv) {
		Ninv = ninv;
	}

}
